package com.zoagroup.funzy.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;


/**
 * The persistent class for the activity database table.
 * 
 */
@Entity
public class Activity implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name="id_activity", unique=true, nullable=false)
	private Integer idActivity;

	private Integer category;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name="creation_date")
	private Date creationDate;

	private String description;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name="end_date")
	private Date endDate;

	@Column(name="is_group")
	private Boolean isGroup;
	
	@Column(name="is_presential")
	private Boolean isPresential;
	
	private String name;

	private String photo;

	private String prize;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name="start_date")
	private Date startDate;

	private Boolean status;

	//bi-directional many-to-one association to TypeAtv
	@ManyToOne(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
	@JoinColumn(name="type_atv")
	private TypeAtv typeAtvBean;

	//bi-directional many-to-one association to User
	@ManyToOne(cascade=CascadeType.ALL)
	@JoinColumn(name="id_user")
	private User user;

	//bi-directional one-to-one association to Ranking
	@OneToOne(mappedBy="activity",cascade=CascadeType.ALL,fetch=FetchType.LAZY)
	private Ranking ranking;

	//bi-directional many-to-one association to Stage
	@OneToMany(mappedBy="activity",cascade=CascadeType.ALL,fetch=FetchType.LAZY)
	private List<Stage> stages;

	//bi-directional many-to-many association to User
	@ManyToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
	@JoinTable(name="user_activity"
				, joinColumns={@JoinColumn(name="id_activity")}
				, inverseJoinColumns={@JoinColumn(name="id_user")})
	private List<User> participatingUsers;

	public Activity() {
	}

	public Integer getIdActivity() {
		return this.idActivity;
	}

	public void setIdActivity(Integer idActivity) {
		this.idActivity = idActivity;
	}

	public Integer getCategory() {
		return this.category;
	}

	public void setCategory(Integer category) {
		this.category = category;
	}

	public Date getCreationDate() {
		return this.creationDate;
	}

	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}

	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Date getEndDate() {
		return this.endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public Boolean getIsGroup() {
		return this.isGroup;
	}

	public void setIsGroup(Boolean isGroup) {
		this.isGroup = isGroup;
	}

	public Boolean getIsPresential() {
		return isPresential;
	}

	public void setIsPresential(Boolean isPresential) {
		this.isPresential = isPresential;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPhoto() {
		return this.photo;
	}

	public void setPhoto(String photo) {
		this.photo = photo;
	}

	public String getPrize() {
		return this.prize;
	}

	public void setPrize(String prize) {
		this.prize = prize;
	}

	public Date getStartDate() {
		return this.startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Boolean getStatus() {
		return this.status;
	}

	public void setStatus(Boolean status) {
		this.status = status;
	}

	public TypeAtv getTypeAtvBean() {
		return this.typeAtvBean;
	}

	public void setTypeAtvBean(TypeAtv typeAtvBean) {
		this.typeAtvBean = typeAtvBean;
	}

	public User getUser() {
		return this.user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Ranking getRanking() {
		return this.ranking;
	}

	public void setRanking(Ranking ranking) {
		this.ranking = ranking;
	}

	public List<Stage> getStages() {
		return this.stages;
	}

	public void setStages(List<Stage> stages) {
		this.stages = stages;
	}

	public Stage addStage(Stage stage) {
		if(this.stages == null) {
			this.stages = new LinkedList<Stage>();
		}
		
		getStages().add(stage);
		stage.setActivity(this);

		return stage;
	}

	public Stage removeStage(Stage stage) {
		if(this.stages == null) {
			this.stages = new LinkedList<Stage>();
		}
		
		getStages().remove(stage);
		stage.setActivity(null);

		return stage;
	}

	public List<User> getParticipatingUsers() {
		return this.participatingUsers;
	}

	public void setParticipatingUsers(List<User> participatingUsers) {
		this.participatingUsers = participatingUsers;
	}

	@Override
	public String toString(){
		String rt = "";
		
		if(this != null) {
			rt = "ID: " + this.idActivity;
			rt += "\nNAME: " + this.name;
			rt += "\nDESCRIPTION: " + this.description;
			rt += "\nTYPE: " + this.getTypeAtvBean().getDescription();
			rt += "\nOWNER: " + this.getUser().getName(); 
		}
		
		return rt;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((idActivity == null) ? 0 : idActivity.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Activity other = (Activity) obj;
		if (idActivity == null) {
			if (other.idActivity != null)
				return false;
		} else if (!idActivity.equals(other.idActivity))
			return false;
		return true;
	}
}